package rpc.utils;

import java.util.Map;
import java.util.Set;

/*********
 *
 *
 *@author 197
 *@date 2020/6/19 16:07
 *
 **/


public class TypeSafeMap {

  private Map<String, Object> body;

  public TypeSafeMap(Map<String, Object> body) {
    this.body = body;
  }

  public Map<String, Object> get() {
    return this.body;
  }

  public boolean contains(String name) {
    return this.body.containsKey(name);
  }

  public void remove(String name) {
    this.body.remove(name);
  }

  public Set<String> keySet() {
    return this.body.keySet();
  }

  public String stringValue(String name, String dv) {
    String v = this.stringValue(name);
    return v == null ? dv : v;
  }

  public String stringValue(String name) {
    Object v = this.body.get(name);
    return TypeSafe.anyToString(v);
  }

  public String notNullString(String name) {
    String v = this.stringValue(name);
    return v == null ? "" : v;
  }

  public String notNullString(String name, String dv) {
    String v = this.stringValue(name);
    return v != null && !v.equals("") ? v : dv;
  }

  public int intValue(String name) {
    Object v = this.body.get(name);
    return TypeSafe.anyToInt(v);
  }

  public int intValue(String name, int dv) {
    Object v = this.body.get(name);
    return TypeSafe.anyToInt(v, dv);
  }

  public long longValue(String name) {
    Object v = this.body.get(name);
    return TypeSafe.anyToLong(v);
  }

  public long longValue(String name, long dv) {
    Object v = this.body.get(name);
    return TypeSafe.anyToLong(v, dv);
  }

  public double doubleValue(String name) {
    Object v = this.body.get(name);
    return TypeSafe.anyToDouble(v);
  }

  public double doubleValue(String name, double dv) {
    Object v = this.body.get(name);
    return TypeSafe.anyToDouble(v, dv);
  }

  public boolean boolValue(String name) {
    Object v = this.body.get(name);
    return TypeSafe.anyToBool(v);
  }

  public boolean boolValue(String name, boolean dv) {
    Object v = this.body.get(name);
    return TypeSafe.anyToBool(v, dv);
  }

  public Map<String, Object> mapValue(String name) {
    Object v = this.body.get(name);
    return TypeSafe.anyToMap(v);
  }
}